/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dal;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.logging.Level;
import java.util.logging.Logger;

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

/**
 *
 * @author Stanley
 */
public class SqlDatabase implements IDatabaseConfig {

//    private static Connection connection = null;
    public static String configFile = null;

    public static Connection getInstance() {
        // singleton pattern
        //if (connection == null) {
        try {
            Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(configFile));
            doc.getDocumentElement().normalize();
            String dbhost = doc.getElementsByTagName("host").item(0).getTextContent();
            String dbport = doc.getElementsByTagName("port").item(0).getTextContent();
            String dbuser = doc.getElementsByTagName("user").item(0).getTextContent();
            String dbpass = doc.getElementsByTagName("pass").item(0).getTextContent();
            String dbname = doc.getElementsByTagName("database").item(0).getTextContent();
            String dbconnString = "jdbc:mysql://" + dbhost + ":" + dbport + "/" + dbname + "?setUnicode=true&characterEncoding=UTF-8";
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            return DriverManager.getConnection(dbconnString, dbuser, dbpass);
            //query = (Statement) dbconn.createStatement();
        } catch (Exception ex) {
            Logger.getLogger(SqlDatabase.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
        //}
    }

    public Object getAllDataFrom(String table, String condition) {
        return null;
    }

    public Object getOneDataFrom(String table, String condition) {

        return null;
    }
}
